package com.xinghai.system.service.impl;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import com.xinghai.common.utils.DateUtils;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.common.constant.GenConstants;
import com.xinghai.system.mapper.QdPayOrderMapper;
import com.xinghai.system.domain.QdPayOrder;
import com.xinghai.system.service.IQdPayOrderService;

/**
 * 对账单Service业务层处理
 *
 * @author xinghai
 * @date 2024-12-07
 */
@Service
public class QdPayOrderServiceImpl extends ServiceImpl<QdPayOrderMapper, QdPayOrder>
        implements IQdPayOrderService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询对账单列表
     *
     * @param qdPayOrder 对账单
     * @return 对账单集合
     */
    @Override
    public List<QdPayOrder> list(QdPayOrder qdPayOrder) {
        QueryWrapper<QdPayOrder> queryWrapper = new QueryWrapper<>();
        if (qdPayOrder.getStatus()!= null  && !qdPayOrder.getStatus().isEmpty()){
            queryWrapper.eq("status", qdPayOrder.getStatus());
        }
        if (qdPayOrder.getParams().get("beginCreateTime")!= null && !"".equals(qdPayOrder.getParams().get("beginCreateTime"))
        &&qdPayOrder.getParams().get("endCreateTime")!= null && !"".equals(qdPayOrder.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", qdPayOrder.getParams().get("beginCreateTime"),qdPayOrder.getParams().get("endCreateTime"));
        }
        if (qdPayOrder.getGenDate()!= null ){
            queryWrapper.apply("date_format(gen_date,'%Y-%m-%d') = '"+ DateUtils.dateTime(qdPayOrder.getGenDate()) +"'");
        }
        if (qdPayOrder.getStoreId()!= null ){
            queryWrapper.eq("store_id", qdPayOrder.getStoreId());
        }
        if (qdPayOrder.getStoreName()!= null  && !qdPayOrder.getStoreName().isEmpty()){
            queryWrapper.like("store_name", qdPayOrder.getStoreName());
        }
        return this.list(queryWrapper);
    }

    /**
     * 删除对账单信息
     * @param id 需要删除的数据ID
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    public boolean delete(Long id, boolean flag) {
        if (flag){
            QdPayOrder qdPayOrder = new QdPayOrder();
            qdPayOrder.setId(id);
            qdPayOrder.setDelFlag(GenConstants.DELETE_FLAG_YES);
            return this.updateById(qdPayOrder);
        }
        return this.removeById(id);
    }

    /**
     * 批量删除对账单信息
     *
     * @param ids 需要删除的数据ID集合
     * @param flag 是否是逻辑删除
     * @return 结果
     */
    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids, boolean flag) {
        if (flag){
            List<QdPayOrder> collect = Arrays.stream(ids).map(m -> {
                QdPayOrder qdPayOrder = new QdPayOrder();
                qdPayOrder.setId(m);
                qdPayOrder.setDelFlag(GenConstants.DELETE_FLAG_YES);
                return qdPayOrder;
            }).collect(Collectors.toList());
            return this.updateBatchById(collect);
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 导入对账单信息
     *
     * @param list 对账单信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<QdPayOrder> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            QdPayOrderMapper qdPayOrderMapper = sqlSession.getMapper(QdPayOrderMapper.class);
            list.forEach(qdPayOrderMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
